import { throttle } from 'miniprogram-licia'
Page({
  data: {
    visitorList: [],
    isEmpty: false,
    hasMore: false
  },
  onLoad () {
    this.getVisitorList()
    this.getMoreVisitor = throttle(() => {
      if (!this.data.hasMore) return
      this.getVisitorList(++this._current)
    }, 100)
  },
  async getVisitorList (current=1, pageSize=5) {
    const {code, data: {pageTotal, rows: visitorList}} = await wx.http.get('/visitor', {
      current,
      pageSize
    })
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      visitorList: this.data.visitorList.concat(visitorList),
      isEmpty: visitorList.length === 0,
      hasMore: pageTotal > current
    })
    this._current = current
  },
  goPassport(ev) {
    wx.navigateTo({
      url: '/visitor_pkg/pages/passport/index?id=' + ev.mark.id,
    })
  },
})
